<template>
	<view class="myInfo">
		<z-nav-bar>新增请假申请</z-nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<!--<public-module></public-module>-->
		<view class="forms">
			<uni-forms :modelValue="formData" ref="form">
				<uni-forms-item label="请假类型" name="name">
					<view class="select">
						<picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
							<view class="uni-input">{{array[qjIndex].name}}
								<uni-icons type="arrowright" size="16"></uni-icons>
							</view>
						</picker>
					</view>
				</uni-forms-item>
				<uni-forms-item label="开始时间" name="phone">
					<view class="select">
						<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindStartDateChange">
							<view class="uni-input">{{date}}
								<uni-icons type="arrowright" size="16"></uni-icons>
							</view>
						</picker>
					</view>
				</uni-forms-item>
				<uni-forms-item label="结束时间" name="phone">
					<view class="select">
						<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindEndDateChange">
							<view class="uni-input">{{date}}
								<uni-icons type="arrowright" size="16"></uni-icons>
							</view>
						</picker>
					</view>
				</uni-forms-item>
				<uni-forms-item>
					 <view class="title">请假事由</view>
					 <textarea class="reason" v-model="txt" maxlength="100" placeholder="请输入"/>
				</uni-forms-item>
				<uni-forms-item>
					<view class="title">病假、公假请拍照上传（0-3张）</view>
					<view class="cu-form-group">
						<view class="grid col-4 grid-square flex-sub" style="display: flex;">
							<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
							 <image class="up-img" :src="imgList[index]" mode="aspectFill"></image>
								<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
									<icon type="clear" size="16"/>
								</view>
							</view>
							<view class="solids" @tap="ChooseImage" v-if="imgList.length<3">
								<text class='cuIcon-cameraadd'></text>
							</view>
						</view>
					</view>
				</uni-forms-item>
			</uni-forms>
		</view>
		<button type="primary" class="btn" @click="submitForm">提交</button>
	</view>
</template>

<script>
	function getDate(type) {
		const date = new Date();
	
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
	
		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;
		day = day > 9 ? day : '0' + day;
		return `${year}-${month}-${day}`;
	}
	export default {
		components: {

		},
		data() {
			return {
				formData: {
					name: '',
					phone: '',
					QQ: '',
					school: '请选择',
					major: '请选择',
					grade: '请选择',
					profession: '',
					class: '',
				},
				array: [{name:'事假'},{name: '病假'}, {name:'调休'}, {name:'婚假'}],
				qjIndex: 0,
				date: getDate('now'),
				startDate:getDate('start'),
				endDate:getDate('end'),
				dateEnd: getDate('now'),
				txt: '',
				imgList: [],
				modalName: null,
				index:-1,

			};
		},
		onLoad(e) {
			console.log({
				e
			})
		},
		onShow() {

		},
		//方法
		methods: {
			 //选择图片
			ChooseImage() {
				uni.chooseImage({
					count: 3, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			//预览图片
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			//删除图片
			DelImg(e) {
				uni.showModal({
					title: '删除照片',
					content: '确定要删除这张照片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			bindPickerChange(e) {
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.qjIndex = e.detail.value
			},
			bindStartDateChange(e) {
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.date = e.detail.value
			},
			bindEndDateChange(e) {
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.dateEnd = e.detail.value
			},
			onPageJump(url) {
				// console.log(type)
				uni.navigateTo({
					url: '/subpages/my/search?params=' + encodeURIComponent(JSON.stringify(this.formData))
				});
			},
			submitForm(form) {
				// 手动提交表单
				this.$refs.form.submit().then((res) => {
					console.log('表单返回得值：', res)
				})
			},
		},
		//页面下来刷新
		onPullDownRefresh() {},
		//页面上拉触底
		onReachBottom() {},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss">
	@import '@/style/mixin.scss';

	page {
		background-color: $uni-bg-color-grey;
	}

	.myInfo {
		position: relative;
		height: 100%;
		width: 100%;
		padding: 16px;
		box-sizing: border-box;
	}

	.forms {
		width: 686upx;
		margin: 0 auto;
		padding: 24px 16px;
		background: #FEFFFF;
		border-radius: 16px;

		/deep/ input {
			text-align: right;
			outline: none;
		}

		/deep/ .is-input-border {
			border: 0 !important;
		}

		.select {
			width: 100%;
			text-align: right;
			line-height: 72upx;
		}
	}

	.btn {
		background-color: $uni-theme-color;
		border-radius: 30px;
		margin-top: 36upx;
	}

	.screenfull {
		position: absolute;
		background: #fff;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 3;
	}
	/deep/ .input-value-border{
		border:0;
	}
	.reason {
		background: #F7F8FA;
		width: 100%;
		padding: 10px;
		margin-top: 13px;
		height: 96px;
	}
	.solids {
		width: 30%;
		background: #F7F8FA;
		height: 80px;
	}
	.cu-form-group {
		margin-top: 10px;
	}
	.bg-img {
		width: 30%;
		height: 80px;
		margin-right: 10px;
		position: relative;
		.up-img {
			width: 100%;
			height: 100%;
		}
		.cu-tag {
			position: absolute;
			right: 5px;
			top: 5px;
		}
	}
	
</style>
